package com.jmyd.commons.base.vo;

import com.github.pagehelper.Page;
import com.jmyd.commons.base.dto.PageParam;
import lombok.Data;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

/**
 * 公共分页查询返回对象
 *
 * @author samphin
 * @date 2019-10-16 10:35:39
 */
@Data
public class PageBean<T> implements Serializable {

    private static final long serialVersionUID = -6506132925546545528L;

    /**
     * 当前页码
     */
    private int currentPage;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 总记录数
     */
    private long count;

    /**
     * 结果集
     */
    private Collection<T> list = Collections.emptyList();

    public PageBean() {

    }

    public PageBean(Collection<T> list, long total, int totalPage) {
        this.list = list;
        this.count = total;
        this.totalPage = totalPage;
    }

    public PageBean(Collection<T> list, Page page) {
        this.list = list;
        this.count = page.getTotal();
        this.totalPage = page.getPages();
        this.currentPage = page.getPageNum();
        this.pageSize = page.getPageSize();
    }

    public PageBean(int currentPage, int pageSize, Collection<T> entities, long count) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.list = entities;
        this.count = count;
    }

    public PageBean(Collection<T> entities, PageParam pageParam, long count) {
        this.currentPage = pageParam.getPageNum();
        this.pageSize = pageParam.getPageSize();
        this.list = entities;
        this.count = count;
        /* 公式= (总记录数+页大小-1)/页大小 */
        this.totalPage = (int) ((count + pageSize - 1) / pageSize);
    }

    public PageBean(Integer currentPage, Integer pageSize) {
        if (currentPage == null || currentPage <= 0) {
            currentPage = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }
}
